package com.flywen.generator;

import com.flywen.generator.common.DBName;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * @Author
 * @Create 2019-01-24 13:52
 * @Version 1.0
 * @Description
 */
public class Generator {
    static Properties properties = new Properties();
    public static DBName dbName = DBName.MYSQL;

    static {
        try {
            properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("generator.properties"));
            String driverClass = Generator.properties.getProperty("jdbc.driver");
            if (driverClass.toLowerCase().contains("oracle")){
                dbName= DBName.ORACLE;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Generator() {
    }

    private static Connection getConnection() throws ClassNotFoundException, SQLException {

        String driverClass = properties.getProperty("jdbc.driver");
        String url = properties.getProperty("jdbc.url");
        Class.forName(driverClass);
        Connection connection = DriverManager.getConnection(url, properties.getProperty("username"), properties.getProperty("password"));
        if (connection == null) {
            throw new RuntimeException("connection is null, Please check the driver settings!");
        } else {
            return connection;
        }
    }

    private static List<TableEntry> readTableEntries() throws IOException {
        String tables = properties.getProperty("tableName");
        List<TableEntry> entries = new ArrayList();
        String[] var2 = tables.split(",");
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String table = var2[var4];
            TableEntry te = new TableEntry();
            te.setTableName(table);
            te.setPackageName(properties.getProperty("packageName") + "." + Utils.getSubPackage(table));
            te.setPrefixName(Utils.getSubPackage(table));
            entries.add(te);
        }

        return entries;
    }

    public static void execute() throws IOException, Exception {
        Connection connection = getConnection();
        List<TableEntry> entries = readTableEntries();
        System.out.println("There is(are) " + entries.size() + " table(s) to generate!");

        String tableName;
        for(Iterator var2 = entries.iterator(); var2.hasNext(); System.out.println("生成表   " + tableName + " 成功...")) {
            TableEntry entry = (TableEntry)var2.next();
            String packageName = entry.getPackageName();
            tableName = entry.getTableName();
            String sequenceName = entry.getSequenceName();
            tableName = tableName.toUpperCase();
            if (sequenceName != null) {
                sequenceName = sequenceName.toUpperCase();
            }

            ParserInfo parserInfo;
            if (properties.getProperty("generatorTarget").indexOf("po") != -1) {
                parserInfo = new ParserInfo(connection, packageName, tableName, sequenceName);
                ModelMaker modelMaker = new ModelMaker(parserInfo);
                modelMaker.makeModel();
            }

            parserInfo = new ParserInfo(connection, packageName, tableName, sequenceName, properties.getProperty("entityPackageName") + "." + entry.getPrefixName());
            if (properties.getProperty("generatorTarget").indexOf("xml") != -1) {
                XmlMaker xmlMaker = new XmlMaker(parserInfo);
                xmlMaker.makeDao(tableName);
            }

            if (properties.getProperty("generatorTarget").indexOf("dao") != -1) {
                MapperMaker daoMaker = new MapperMaker(parserInfo);
                daoMaker.makeInterface();
            }

//            if (properties.getProperty("generatorTarget").indexOf("service") != -1) {
//                ServiceInterfaceMaker serviceInterfaceMaker = new ServiceInterfaceMaker(parserInfo);
//                serviceInterfaceMaker.makeModel();
//                ServiceImplMaker serviceImplMaker = new ServiceImplMaker(parserInfo);
//                serviceImplMaker.makeModel();
//            }
        }

        connection.close();
    }
    
    public static void main(String[] args) throws Exception {
        //Generator.execute();
    }

}
